#include <iostream>
#include <list>
#include "LinkedList.h"
using namespace std;
//默认构造函数，构造一个逻辑为空的链表
LinkedList::LinkedList() {
    head = nullptr;
    size = 0;
}
//拷贝构造函数，构造一个逻辑上与参数内容相同的链表
LinkedList::LinkedList(const LinkedList&rhs) {
    Node *prll = rhs.head;//记录 rhs 的需要复制的节点的指针
    Node *temp = nullptr;//创建节点用到的临时变量
    Node *last = nullptr;//记录最后一个位置的指针，尾插时直接将节点插道到 last 的后面
    head = nullptr;//给头指针复制【指针都必须要复制】
    size = rhs.size;//确定链表大小
    while (prll != nullptr) {
        temp = new Node();
        if (head == nullptr) {
            head = temp;
        }
        temp->data = prll->data;
        temp->next = nullptr;
        if (last == nullptr) {
            last = temp;
        } else {
            last->next = temp;
            last = last->next;
        }
        prll = prll->next;
    }
}
//原生数组构造函数，构造一个内容与给定数组相同的链表
LinkedList::LinkedList(int const a[],int n) {
    //cout << "1" << endl;
    Node *last = nullptr;
    Node *temp = nullptr;
    head = nullptr;
    size = n;
    for (int i = 0; i < n; i++) {
        temp = new Node();
        if (head == nullptr) {
            head = temp;
        }
        temp->data = a[i];
        temp->next = nullptr;
        if (last == nullptr) {
            last = temp;
        } else {
            last->next = temp;
            last = last->next;
        }
    }
}
//填充构造函数，构造一个内容为n个value的链表
LinkedList::LinkedList(int n,int value) {
    Node *last = nullptr;
    Node *temp = nullptr;
    head = nullptr;
    size = n;
    for (int i = 0; i < n; i++) {
        temp = new Node();
        if (head == nullptr) {
            head = temp;
        }
        temp->data = value;
        temp->next = nullptr;
        if (last == nullptr) {
            last = temp;
        } else {
            last->next = temp;
            last = last->next;
        }
    }
}
//析构函数，一定要自行实现，否则有内存泄漏
LinkedList::~LinkedList() {
    while (head->next) {
        Node* toDel = head->next;
        head->next = toDel->next;
        delete toDel;
    }
}
//无话可说


void LinkedList::insert(int pos,int value) {
    if ((0 <= pos) && (pos <= size)) {
        Node* node = new Node();
        if (node != nullptr) {
            Node* current = head;
            for (int i = 0; i < pos; ++i) {
                current = current->next;
            }
            node->data = value;
            node->next = current->next;
            current->next = node;
            size++;
        }
    }
}

void LinkedList::remove(int pos) {
    if ((0 <= pos) && (pos <= size)) {
        Node* current = head;
        for (int i = 0; i < pos; ++i) {
            current = current->next;
        }
        Node* toDel = current->next;
        current->next = toDel->next;
        size--;
        delete toDel;
    }
}

int LinkedList::at(int pos)const {
    Node* current = head;
    for (int i = 0; i < pos; i++) {
        current = current->next;
    }
    return current->next->data;
}

void LinkedList::modify(int pos, int newValue) {
    Node* current = head;
    for (int i = 0; i < pos; i++) {
        current = current->next;
    }
    current->next->data = newValue;
}

void LinkedList::disp()const {
    Node* temp = head;
    while (temp != nullptr) {
        cout << temp->data << " ";
    }
    cout << endl;
}

LinkedList::Node* LinkedList::advance(int pos) const {
    //cout << 1;
    Node * pre = head;
    for (int i = 0; i < pos; i++) {
        pre = pre->next;
    }
    return pre;
}
